package Views;

import Controllers.*;
import Controllers.SearchController;
import Views.TableModels.TableModelAbstract;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Picker <T extends DB.RowInterface> extends javax.swing.JFrame
{
	private SearchController<T> searchController = new SearchController<T>();
	private ControllerInterface<T> controller;
	private TableModelAbstract<T> tableModel;
	private T chosenOne = null;

	/**
	 * Starter en listSelectionListener og mouseListener i pickeren
	 * @param controller Den givne controller
	 * @param tableModel Den givne tableModel
	 */
	public Picker(ControllerInterface<T> controller, TableModelAbstract<T> tableModel)
	{
		initComponents();
		
		this.controller = controller;
		this.tableModel = tableModel;

		resultTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e)
			{
				if(resultTable.getSelectedRowCount() == 1)
				{
					chooseB.setEnabled(true);
				}
				else
				{
					chooseB.setEnabled(false);
				}
			}
		});
		resultTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(resultTable.getSelectedRowCount() == 1)
					{
						chooseBActionPerformed();
					}
				}
			}
		});
	}

	/**
	 * Vælger den markeret element på listen og lukker picker vinduet
	 */
	private void chooseBActionPerformed()
	{
		int chosenId = Integer.parseInt(resultTable.getValueAt(resultTable.getSelectedRow(), 0).toString());
		this.chosenOne = controller.find(chosenId);
		this.doClose();
	}

	/**
	 * Starter søgning
	 */
	private void searchBActionPerformed()
	{
		String searchText = searchTF.getText();
		ArrayList<T> results = null;

		if(controller instanceof CustomerController)
		{
			results = (ArrayList<T>)searchController.searchCustomer(searchText);
		}
		else if(controller instanceof LocationController)
		{
			results = (ArrayList<T>)searchController.searchLocation(searchText);
		}
		else if(controller instanceof RouteController)
		{
			results = (ArrayList<T>)searchController.searchRoute(searchText);
		}
		else if(controller instanceof RoundController)
		{
			results = (ArrayList<T>)searchController.searchRound(searchText);
		}
		else if(controller instanceof EmployeeController)
		{
			results = (ArrayList<T>)searchController.searchEmployee(searchText);
		}
		else if(controller instanceof CarController)
		{
			results = (ArrayList<T>)searchController.searchCar(searchText);
		}
		else if(controller instanceof ScheduleController)
		{
			results = (ArrayList<T>)searchController.searchSchedule(searchText);
		}

		tableModel.setData(results);
		resultTable.setModel(tableModel);
	}

	/**
	 * Lukker picker vinduet
	 */
	private void cancelBActionPerformed()
	{
		this.doClose();
	}

	/**
	 * Lukker picker vinduet
	 */
	private void doClose()
	{
		WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
		processWindowEvent(evt);
	}

	/**
	 * Returnerer chosenOne
	 * @return chosenOne
	 */
	public T getTheChosenOne()
	{
		return this.chosenOne;
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		searchL = new javax.swing.JLabel();
		searchTF = new javax.swing.JTextField();
		searchB = new javax.swing.JButton();
		resultSP = new javax.swing.JScrollPane();
		resultTable = new javax.swing.JTable();
		chooseB = new javax.swing.JButton();
		cancelB = new javax.swing.JButton();

		setName("Form"); // NOI18N

		searchL.setText("Søgeord:");
		searchL.setName("searchL"); // NOI18N

		searchTF.setName("searchTF"); // NOI18N

		searchB.setText("Søg");
		searchB.setName("searchB"); // NOI18N
		searchB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				searchBActionPerformed();
			}
		});

		resultSP.setName("resultSP"); // NOI18N

		resultTable.setModel(new Views.TableModels.LocationTableModel());
		resultTable.setName("resultTable"); // NOI18N
		resultSP.setViewportView(resultTable);

		chooseB.setText("Vælg");
		chooseB.setName("chooseB"); // NOI18N
		chooseB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				chooseBActionPerformed();
			}
		});

		cancelB.setText("Annuller");
		cancelB.setName("cancelB"); // NOI18N
		cancelB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelBActionPerformed();
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addComponent(resultSP, 0, 0, Short.MAX_VALUE)
					.addGroup(layout.createSequentialGroup()
						.addComponent(searchL)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(searchB))
					.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addComponent(cancelB)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(chooseB)))
				.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(searchB)
					.addComponent(searchL))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(resultSP, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(chooseB)
					.addComponent(cancelB))
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);

		pack();
	}// </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JButton cancelB;
	private javax.swing.JButton chooseB;
	private javax.swing.JScrollPane resultSP;
	private javax.swing.JTable resultTable;
	private javax.swing.JButton searchB;
	private javax.swing.JLabel searchL;
	private javax.swing.JTextField searchTF;
	// End of variables declaration
}